package controllers

import (
	"dmsv/process"
	"dmsv/reqcache"
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/core/logs"
)

type CallController struct {
	BaseController
}

func (this *CallController) URLMapping() {
	this.Mapping("anotify", this.Notify)
	this.Mapping("verifycode", this.VerifyCode)
	this.Mapping("callback", this.CallBack)
}

// @Title
// @Description
// @Param   callId            query   string    true      "callId"
// @Param   callee            query   string    true      "被叫号码"
// @Param   playTimes         query   string    true      "播放次数"
// @Param   callCanTime       query   string    true      "等待时长"
// @Param   lastDelayTime     query   string    true      "播放完成后多久挂机"
// @Param   brand             query   string    true      "品牌"
// @Param   queueID           query   string    true      "队列Id"
// @Param   notifyFileName    query   string    true      "放音文件"
// @Param   toSerNum          query   string    true      "主叫号码 "
// @Param   developerId       query   string    true      "开发者Id "
// @Success 200 {object}   controllers.Result
// @router /anotify [get]
func (this *CallController) Notify() {

	queueID := this.GetString("queueID", "1")
	callId := this.GetString("callId")
	if len(callId) == 0 {
		this.Data["json"] = &Result{"001", callId, queueID}
		this.ServeJSON()
		logs.Error("no callId info:%v", this.URLMapping)
		return
	}

	callee := this.GetString("callee")
	playTimes, err := this.GetInt("playTimes", 0)
	if err != nil {
		playTimes = 0
		logs.Error("playTimes value get failed:%v", err)
	}

	caller := this.GetString("toSerNum")
	fileName := this.GetString("notifyFileName")
	waitTime, err := this.GetInt("callCanTime", 60)
	if err != nil {
		waitTime = 60
		logs.Error("waitTime value get failed:%v", err)
	}

	delayTime, err := this.GetInt("lastDelayTime", 0)
	if err != nil {
		logs.Error("delayTime value get failed:%v", err)
	}

	developerId := this.GetString("developerId", "")

	logs.Debug("receive new notice.callId:%v caller:%v,callee:%v,playfile:%v,delay:%v,devId:%s",
		callId, caller, callee, fileName, delayTime, developerId)

	args := process.ArgsOfNotify{
		CallId:   callId,
		Caller:   caller,
		Callee:   callee,
		PlayTime: playTimes,
		PlayFile: fileName,
		Method:   process.NotifyNotice,
		Wait:     waitTime,
		Delay:    delayTime,
		DevId:    developerId,
	}

	err = process.CallNotify(args)
	if nil == err {
		this.Data["json"] = &Result{"00000", callId, queueID}
	} else {
		this.Data["json"] = &Result{"10001", callId, queueID}
	}

	this.ServeJSON()

	return
}

// @Title
// @Description
// @Param   callId             query   string    true      "callId"
// @Param   calledNumber       query   string    true      "被叫号码"
// @Param   playTimes          query   string    true      "播放次数"
// @Param   callCanTime        query   string    true      "等待时长"
// @Param   language           query   string    true      "语言"
// @Param   ts                 query   string    true      "时间戳"
// @Param   brand              query   string    true      "品牌"
// @Param   verifyCode         query   string    true      "放音文件"
// @Param   callDisplayNumber          formData   string    true      "主叫号码 "
// @Success 200 {object}   controllers.Result
// @router /verifycode [get]
func (this *CallController) VerifyCode() {

	queueID := this.GetString("queueID", "1")
	callId := this.GetString("callId")
	if len(callId) == 0 {
		this.Data["json"] = &Result{"001", callId, queueID}
		this.ServeJSON()
		logs.Error("no callId info")
		return
	}
	callee := this.GetString("calledNumber")
	playTimes, err := this.GetInt("playTimes", 0)
	if err != nil {

		playTimes = 0
		logs.Error("playTimes value get failed:%v", err)
	}

	caller := this.GetString("callDisplayNumber")
	fileName := this.GetString("verifyCode")
	waitTime, err := this.GetInt("callCanTime", 60)
	if err != nil {
		waitTime = 60
		logs.Error("waitTime value get failed:%v", err)
	}

	delayTime, err := this.GetInt("lastDelayTime", 0)
	if err != nil {
		delayTime = 0
		logs.Error("delayTime value get failed:%v", err)
	}

	args := process.ArgsOfNotify{
		CallId:   callId,
		Caller:   caller,
		Callee:   callee,
		PlayTime: playTimes,
		PlayFile: fileName,
		Method:   process.NotifyVerify,
		Wait:     waitTime,
		Delay:    delayTime,
	}
	err = process.CallNotify(args)
	if err == nil {
		this.Data["json"] = &Result{"00000", callId, queueID}
	} else {
		this.Data["json"] = &Result{"10001", callId, queueID}
	}

	this.ServeJSON()

	return
}

// @Title 坐席外呼
// @Description 坐席外呼
// @Param   param   body   controllers.SipCallBack     true   "reqId为请求Id,10s内唯一，用以规避重复请求"
// @Success 200 	{object}   controllers.ResultBs
// @router /callback [post]
func (this *CallController) CallBack() {

	params := SipCallBack{}
	logs.Info("new call back: %v", string(this.Ctx.Input.RequestBody))
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &params); err != nil {

		logs.Error("json failed:%v", err)
		this.Data["json"] = ParamsError
		this.ServeJSON()
		return
	}

	if !reqcache.IsValid(params.ReqId) {
		errmsg := fmt.Sprintf("reqId %v is request before", params.ReqId)
		this.Data["json"] = &ResultBs{10000, errmsg, ""}
		this.ServeJSON()
		return
	}

	callId, err := process.CallBackService(params.AppId, params.Service, params.Number, params.Display, "")
	if err == nil {
		this.Data["json"] = &ResultBs{0, "success", callId}
	} else {
		this.Data["json"] = &ResultBs{10001, err.Error(), ""}
	}

	this.ServeJSON()
}
